package com.ycrlkj.android.modulescore.main.activity;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.launcher.ARouter;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.ycrlkj.android.modulescore.main.R;
import com.ycrlkj.android.modulescore.main.databinding.FragmentPayBinding;
import com.ycrlkj.android.modulespublic.common.constant.RoutePath;
import com.ycrlkj.android.modulespublic.common.utils.xpopup.XPopupUtil;
import com.ycrlkj.android.modulespublic.common.widget.titlebar.MTitleBar;

public class XxAppointmentPayFragment extends Fragment {

    private static Integer amount;

    private FragmentPayBinding binding;

    private XxAppointmentAddActivity activity;

    public XxAppointmentPayFragment() {
    }

    public static XxAppointmentPayFragment newInstance(Integer payAmount) {
        XxAppointmentPayFragment fragment = new XxAppointmentPayFragment();
        amount = payAmount;
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_pay, container, false);
        activity = (XxAppointmentAddActivity) getActivity();

        initTitleBar();
        initBtn();
        initPayAmount();

        return binding.getRoot();
    }

    public void initPayAmount() {
        binding.tvPayAmount.setText(String.valueOf(amount));
    }

    public void initTitleBar() {
        MTitleBar titleBar = binding.mTitleBar;
        titleBar.setBtnEvents(new MTitleBar.BtnEvents() {
            @Override
            public void setBtnLeftOnClickListener() {
                FragmentManager supportFragmentManager = activity.getSupportFragmentManager();
                supportFragmentManager.popBackStackImmediate();
            }

            @Override
            public void setBtnRightOnClickListener() {
            }
        });
    }

    public void initBtn() {
        binding.btnPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoadingPopupView loadingPopupView = XPopupUtil.showLoading(activity);
                XPopupUtil.hideLoadingDelay(loadingPopupView);

                //todo
                //调用微信支付

                //3秒后跳转到home页面
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        XPopupUtil.hideLoading(loadingPopupView);
                        XPopupUtil.showModal(activity, "支付成功, 已完成预约", new OnConfirmListener() {
                            @Override
                            public void onConfirm() {
                                ARouter.getInstance().build(RoutePath.PAGE_XX_APPOINTMENT_LIST).navigation();
                            }
                        });
                    }
                }, 2000);

            }
        });
    }


}